package com.itheima.redbaby.activity;

import com.itheima.redbaby.BaseActivity;
import com.itheima.redbaby.ContantValue;
import com.itheima.redbaby.GloableParams;
import com.itheima.redbaby.R;
import com.itheima.redbaby.BaseActivity.MyHttpTask;
import com.itheima.redbaby.engine.UserEngine;
import com.itheima.redbaby.factory.DaoAndEngineFactory;
import com.itheima.redbaby.utils.PromptManager;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
/**
 * 用户注册界面
 * @author Gaara
 *
 */
public class RegistActivity extends BaseActivity implements OnClickListener {
	private EditText my_name_text;
	private EditText my_psw1_text;
	private EditText my_psw2_text;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.regist_activity);
		myInit();
	}
	/**
	 * 初始化资源
	 */
	private void myInit() {
		TextView head_back_text = (TextView) findViewById(R.id.head_back_text);
		TextView regist_text = (TextView) findViewById(R.id.regist_text);
		
		my_name_text = (EditText) findViewById(R.id.my_name_text);
		my_psw1_text = (EditText) findViewById(R.id.my_psw1_text);
		my_psw2_text = (EditText) findViewById(R.id.my_psw2_text);
		head_back_text.setOnClickListener(this);
		regist_text.setOnClickListener(this);
	}
	@Override
	public void onClick(View v) {
		int id = v.getId();
		switch (id) {
		case R.id.head_back_text://返回
			finish();
			break;
		case R.id.regist_text://注册
			String name = my_name_text.getText().toString();
			String psw1 = my_psw1_text.getText().toString();
			String psw2 = my_psw2_text.getText().toString();
			if(TextUtils.isEmpty(name) || TextUtils.isEmpty(psw1) || TextUtils.isEmpty(psw2)){
				PromptManager.showToast(RegistActivity.this, "用户名或密码不能为空");
				return;
			}else if(!psw1.equals(psw2)){
				PromptManager.showToast(RegistActivity.this, "两次输入密码不一致");
				return;
			}
			regist(name,psw1);
			break;
		}
	}
	/**
	 * 提交数据到后台进行注册
	 * @param name 用户名
	 * @param psw1   密码
	 */
	private void regist(final String name, final String psw1) {
		new MyHttpTask() {
			protected void onPreExecute() {
				PromptManager.showProgressDialog(RegistActivity.this);
//				如果2秒之内没有服务器回应信息,关掉对话框.
				new Handler().postDelayed(new Runnable() {
					
					@Override
					public void run() {
						PromptManager.closeProgressDialog();
					}
				}, 2000);
			};
			@Override
			protected Object doInBackground(Void... params) {
				//模拟链接延迟.
				SystemClock.sleep(500);
				UserEngine impl = DaoAndEngineFactory.getImpl(UserEngine.class);
				return impl.regist(name, psw1);
			}

			@Override
			protected void onPostExecute(Object result) {
				PromptManager.closeProgressDialog();
				if(result != null){
					boolean isLogin = GloableParams.isLogin;
					if(isLogin){
						Intent intent = getIntent();
						int flags = intent.getFlags();
						if(flags == ContantValue.FLAGS_ACCOUNTCENTER_ACTIVITY){
							Intent newIntent = new Intent(RegistActivity.this, AccountCenterActivity.class);
							startActivity(newIntent);
						}
						finish();
					}
					String text = (String) result;
					PromptManager.showToast(RegistActivity.this, text);
				}else{
					PromptManager.showToast(RegistActivity.this, "服务器连接异常,请重试...");
				}
			}
			
			
		}.executeHttp();
	}
	
}
